package com.sec.android.app.myfiles.mockclass;

import com.sec.android.app.myfiles.module.abstraction.FileRecord;

import org.powermock.api.mockito.PowerMockito;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Locale;

import static org.mockito.Matchers.any;
import static org.powermock.api.mockito.PowerMockito.when;

/**
 * Created by jaebae on 2016-04-18.
 */

public class FileMock {
    public static long FILE_MOCK_SIZE = 1024L * 32L; //32KB

    public static File createMock(String path) {
        File fileMock = PowerMockito.mock(File.class);

        when(fileMock.getName()).thenReturn(FileRecord.getName(path));
        when(fileMock.getParent()).thenReturn(FileRecord.getPath(path));
        when(fileMock.getAbsolutePath()).thenReturn(path);
        when(fileMock.getPath()).thenReturn(path);
        when(fileMock.length()).thenReturn(FILE_MOCK_SIZE);
        when(fileMock.lastModified()).thenReturn(1024L);
        if (path.toLowerCase(Locale.getDefault()).contains("dst")) {
            when(fileMock.exists()).thenReturn(false);
        } else if (path.toLowerCase(Locale.getDefault()).contains("dup")) {
            when(fileMock.exists()).thenReturn(!path.contains("1"));
        } else {
            when(fileMock.exists()).thenReturn(true);
        }

        when(fileMock.delete()).thenReturn(true);
        if (FileRecord.getName(path).toUpperCase(Locale.getDefault()).contains("RENAME")) {
            when(fileMock.renameTo(any(File.class))).thenReturn(true);
        } else {
            when(fileMock.renameTo(any(File.class))).thenReturn(false);
        }


        if (FileRecord.getName(path).toUpperCase(Locale.getDefault()).contains("DIR")) {
            File subFile[] = createListFiles(path);
            when(fileMock.listFiles()).thenReturn(subFile);
            when(fileMock.listFiles(any(FilenameFilter.class))).thenReturn(subFile);
            when(fileMock.mkdir()).thenReturn(true);
            when(fileMock.mkdirs()).thenReturn(true);
            when(fileMock.isDirectory()).thenReturn(true);
            when(fileMock.isFile()).thenReturn(false);
            String[] subFileList = createListFile(subFile);
            when(fileMock.list(any(FilenameFilter.class))).thenReturn(subFileList);
            when(fileMock.list()).thenReturn(subFileList);
        } else {
            when(fileMock.mkdir()).thenReturn(false);
            when(fileMock.mkdirs()).thenReturn(false);
            when(fileMock.isDirectory()).thenReturn(false);
            when(fileMock.isFile()).thenReturn(true);
        }

        return fileMock;
    }

    static final int LIST_SUB_FILES_SIZE = 8;
    public static final int LIST_FILES_SIZE = LIST_SUB_FILES_SIZE + 1;
    public static final int NUM_OF_HIDDEN_FILES = 2;


    public static File[] createListFiles(String path) {
        boolean isSub = path.toUpperCase(Locale.getDefault()).contains("SUB_DIR");
        int nLen = LIST_SUB_FILES_SIZE;
        if (!isSub) {
            nLen = LIST_FILES_SIZE;
        }
        File ret[] = new File[nLen];
        for (int i = 0; i < nLen; i++) {
            if (i < NUM_OF_HIDDEN_FILES) {
                ret[i] = createMock(path + "/.test" + i);
            } else {
                ret[i] = createMock(path + "/test" + i);
            }
        }

        if (!isSub) {
            ret[nLen - 1] = createMock(path + "/SUB_DIR");
        }

        return ret;
    }

    public static String[] createListFile(File files[]) {
        if (files == null)
            return null;

        String ret[] = new String[files.length];

        for (int i = 0; i < files.length; i++) {
            ret[i] = files[i].getName();
        }

        return ret;

    }
}
